﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

	

public class UIManager : Singleton<UIManager>
{
	//Dictionary<string, UIbase> dicui = new Dictionary<string, UIbase>();

	/// <summary>
	/// 获取Canvas
	/// </summary>
	private Canvas cas;

	public Canvas Cas
	{
		get { return cas; }
	}


	private Transform BaseUIRoot;      // 位于UI最底层，常驻场景，基础交互
	private Transform PopUIRoot;       // 位于UI上层，弹出式，互斥
	private Transform StoryUIRoot;     // 故事背景层
	private Transform TipUIRoot;       // 位于UI顶层，弹出重要提示信息等
	private Transform MenuPopUIRoot;
	private Transform MessageUIRoot;
	private Transform DeathUIRoot;

	private Dictionary<string, UIbase> m_dicTipUI = new Dictionary<string, UIbase>();
	private Dictionary<string, UIbase> m_dicBaseUI = new Dictionary<string, UIbase>();
	private Dictionary<string, UIbase> m_dicPopUI = new Dictionary<string, UIbase>();
	private Dictionary<string, UIbase> m_dicStoryUI = new Dictionary<string, UIbase>();
	private Dictionary<string, UIbase> m_dicMenuPopUI = new Dictionary<string, UIbase>();
	private Dictionary<string, UIbase> m_dicMessageUI = new Dictionary<string, UIbase>();
	private Dictionary<string, UIbase> m_dicDeathUI = new Dictionary<string, UIbase>();
	//private Dictionary<string, GameObject> m_dicCacheUI = new Dictionary<string, GameObject>();



	public UIManager()
	{
		cas = GameObject.FindObjectOfType<Canvas>();

		m_dicTipUI.Clear();
		m_dicBaseUI.Clear();
		m_dicPopUI.Clear();
		m_dicStoryUI.Clear();
		m_dicMenuPopUI.Clear();
		m_dicMessageUI.Clear();
		m_dicDeathUI.Clear();
		//m_dicCacheUI.Clear();
		
		BaseUIRoot = GameObject.Find("BaseUIRoot").transform;
		if (null == BaseUIRoot)
		{
			OnDebug.Instance.PrintString("没有找到对应父物体" + "BaseUIRoot");
			//BaseUIRoot = AddObjToRoot("BaseUIRoot").transform;
		}

		PopUIRoot = GameObject.Find("PopUIRoot").transform;
		if (null == PopUIRoot)
		{
			OnDebug.Instance.PrintString("没有找到对应父物体" + "PopUIRoot");
			//PopUIRoot = AddObjToRoot("PopUIRoot").transform;
		}

		StoryUIRoot = GameObject.Find("StoryUIRoot").transform;
		if (null == StoryUIRoot)
		{
			OnDebug.Instance.PrintString("没有找到对应父物体" + "StoryUIRoot");
			//StoryUIRoot = AddObjToRoot("StoryUIRoot").transform;
		}

		TipUIRoot = GameObject.Find("TipUIRoot").transform;
		if (null == TipUIRoot)
		{
			OnDebug.Instance.PrintString("没有找到对应父物体" + "TipUIRoot");
			//TipUIRoot = AddObjToRoot("TipUIRoot").transform;
		}

		MenuPopUIRoot = GameObject.Find("MenuPopUIRoot").transform;
		if (null == MenuPopUIRoot)
		{
			OnDebug.Instance.PrintString("没有找到对应父物体" + "MenuPopUIRoot");
			//MenuPopUIRoot = AddObjToRoot("MenuPopUIRoot").transform;
		}

		MessageUIRoot = GameObject.Find("MessageUIRoot").transform;
		if (null == MessageUIRoot)
		{
			OnDebug.Instance.PrintString("没有找到对应父物体" + "MessageUIRoot");
			//MessageUIRoot = AddObjToRoot("MessageUIRoot").transform;
		}

		DeathUIRoot = GameObject.Find("DeathUIRoot").transform;
		if (null == DeathUIRoot)
		{
			OnDebug.Instance.PrintString("没有找到对应父物体" + "DeathUIRoot");
			//DeathUIRoot = AddObjToRoot("DeathUIRoot").transform;
		}

		BaseUIRoot.gameObject.SetActive(true);
		TipUIRoot.gameObject.SetActive(true);
		PopUIRoot.gameObject.SetActive(true);
		StoryUIRoot.gameObject.SetActive(true);
		MenuPopUIRoot.gameObject.SetActive(true);
		MessageUIRoot.gameObject.SetActive(true);
		DeathUIRoot.gameObject.SetActive(true);
	}
	/// <summary>
	/// 打开UI面板
	/// </summary>
	/// <param name="name"></param>
	public void OpenUI(UIPathData _UIPathData)
	{
		UIbase temp = LoadUIbase(_UIPathData);
		temp.Show();
	}
	/// <summary>
	/// 关闭UI面板
	/// </summary>
	/// <param name="name"></param>
	public void Close(UIPathData _UIPathData)
	{

		//UIbase temp = LoadUIbase(_UIPathData);
		//temp.Hide();

		//if(dicui.ContainsKey(_UIPathData.name))
		//{
		//	dicui[_UIPathData.name].Hide();
		//}

		Dictionary<string, UIbase> curDic = null;
		switch (_UIPathData.uiType)
		{
			case UIPathData.UIType.TYPE_BASE:
				curDic = m_dicBaseUI;
				break;
			case UIPathData.UIType.TYPE_POP:
				curDic = m_dicPopUI;
				break;
			case UIPathData.UIType.TYPE_STORY:
				curDic = m_dicStoryUI;
				break;
			case UIPathData.UIType.TYPE_TIP:
				curDic = m_dicTipUI;
				break;
			case UIPathData.UIType.TYPE_MENUPOP:
				curDic = m_dicMenuPopUI;
				break;
			case UIPathData.UIType.TYPE_MESSAGE:
				curDic = m_dicMessageUI;
				break;
			case UIPathData.UIType.TYPE_DEATH:
				curDic = m_dicDeathUI;

				break;
			default:
				return ;
		}

		if (curDic.ContainsKey(_UIPathData.name))
		{
			curDic[_UIPathData.name].Hide();
		}

	}
	/// <summary>
	/// 从字典中获取面板
	/// </summary>
	/// <param name="name"></param>
	/// <returns></returns>
	private UIbase LoadUIbase(UIPathData _UIPathData)
	{

		Dictionary<string, UIbase> curDic = null;
		switch (_UIPathData.uiType)
		{
			case UIPathData.UIType.TYPE_BASE:
				curDic = m_dicBaseUI;
				break;
			case UIPathData.UIType.TYPE_POP:
				curDic = m_dicPopUI;
				break;
			case UIPathData.UIType.TYPE_STORY:
				curDic = m_dicStoryUI;
				break;
			case UIPathData.UIType.TYPE_TIP:
				curDic = m_dicTipUI;
				break;
			case UIPathData.UIType.TYPE_MENUPOP:
				curDic = m_dicMenuPopUI;
				break;
			case UIPathData.UIType.TYPE_MESSAGE:
				curDic = m_dicMessageUI;
				break;
			case UIPathData.UIType.TYPE_DEATH:
				curDic = m_dicDeathUI;

				break;
			default:
				return null;
		}

		if (null == curDic)
		{
			return null;
		}



		UIbase temp;
		if(curDic.TryGetValue(_UIPathData.name,out temp))
		{
			return temp;
		}
		else
		{
			GameObject uitemp = Resources.Load<GameObject>(_UIPathData.name.ToString());
			if(uitemp==null)
			{
				Debug.Log("您要获取的路径不存在");
			}
			else
			{
				GameObject clone = GameObject.Instantiate(uitemp, Cas.transform, false);
				temp = clone.GetComponent<UIbase>();
				if(temp==null)
				{
					Debug.Log("该物体并没有挂载UIbase");
				}
				else
				{
					curDic.Add(_UIPathData.name, temp);
					Debug.Log("第一次打开" + _UIPathData.name.ToString() + "面板");
					temp.Info();
				}
			}
			return temp;
		}
	}
}
